<?php
// +----------------------------------------------------------------------
// | [KyPHP System] Copyright (c) 2020 http://www.kuryun.com/
// +----------------------------------------------------------------------
// | [KyPHP] 并不是自由软件,你可免费使用,未经许可不能去掉KyPHP相关版权
// +----------------------------------------------------------------------
// | Author: fudaoji <fdj@kuryun.cn>
// +----------------------------------------------------------------------

/**
 * Created by PhpStorm.
 * Script Name: Follow.php
 * Create: 2020/6/9 下午9:21
 * Description: 粉丝
 * Author: fudaoji<fdj@kuryun.cn>
 */

namespace app\mp\controller;

class Follow extends Base
{
    /**
     * @var \think\Model
     */
    private $followM;
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->followM = model('mpFollow');
    }

    /**
     * 首页
     * @return mixed
     * @author: fudaoji<fdj@kuryun.cn>
     */
    public function index(){
        $where = ['mpid' => $this->mpId];
        $search_key = input('search_key', '');
        $search_key && $where['nickname'] = ['like', '%'.$search_key.'%'];
        $data = $this->followM->page($this->pageSize, $where, ['update_time' => 'desc'], true, 1);
        $page = $data->appends(['search_key' => $search_key])->render();
        $assign = ['search_key' => $search_key, 'data' => $data, 'page' => $page];
        return $this->show($assign);
    }

    /**
     * 拉取粉丝
     * 一次拉取调用最多拉取10000个关注者的OpenID
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public function pullPost(){
        $next_openid = '';
        $has_more = true;
        $list = [];
        while ($has_more){
            $res = $this->mpApp->user->list($next_openid);
            if($res['count']){
                $openid_list = $res['data']['openid'];
                $follow_list = $this->mpApp->user->select($openid_list)['user_info_list'];
                foreach ($follow_list as $item){
                    controller('mp/follow', 'event')->saveFollow($item, $this->mpId);
                }
                $list = array_merge($list, $follow_list);
                $next_openid = $openid_list[count($openid_list) - 1];
            }else{
                $has_more = false;
            }
        }
    }
}